//
//  YHKLoginModel.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/2/29.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

class YHKLoginModel: NSObject , NSCoding{
    
    /*
    "access_token" = "2.00tykk_D0FXLSd3ac457c614WdgDIC";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 3040292567;
    
    
    返回值字段	字段类型	字段说明
    access_token	string	用于调用access_token，接口获取授权后的access token。
    expires_in	string	access_token的生命周期，单位是秒数。
    remind_in	string	access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
    uid	string	当前授权用户的UID。
    
    */
    
    override init() {
        super.init()
    }
    
    var access_token:String?
    var expires_in:NSTimeInterval = 0{
        
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
        
    }
    var uid:String?
    
    //获取用户接口的时候用的两个数据
    var profile_image_url:String?
    var screen_name:String?
    
    
    //定义一个属性，来记录未来的时间
    var expires_date:NSDate?
    
    //利用KVC
     init(dict:[String: NSObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    
    //防止数据为空的蹦，所以重写下面方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String)
    {}
    
    
    //归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as?String
        
    }
    
    //保存到沙盒
    func addShuJu(file:String){
    //1.获取目录
  let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
//        print(path)
        
        
        
    //2.创建存储的表
    let filePath = (path as NSString).stringByAppendingPathComponent(file)
        
    //3.归档
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
    
    }
    
    
    
    class func redaObject(file:String ) -> YHKLoginModel? {
        
        //1.获取目录
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
//                print(path)
        
        
        
        //2.拼接入径
        let filePath = (path as NSString).stringByAppendingPathComponent(file)
        
        //3.归档
//       return
        
      
        
        let model = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? YHKLoginModel
        
        if let date = model?.expires_date {
            
            if date.compare(NSDate()) == .OrderedDescending{
                
                return model
            }else{
                return nil
            }
            
        }
        
        return nil
    }
    
    
    

}
